package com.nuoniu.sibanyun.controller.finance;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.ReceivableVerification;
import com.nuoniu.sibanyun.entity.finance.VReceivablePayableMain;
import com.nuoniu.sibanyun.service.finance.IVReceivablePayableMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 应收应付单主表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-19
 */
@RestController
@RequestMapping("/finance/receivable")
@Api(tags = "应收应付单主表")
public class VReceivableMainController extends BaseController {

    @Autowired
    private IVReceivablePayableMainService receivableMainService;

    @ApiOperation("获取编码")
    @GetMapping("getCode")
    public HttpResult getCode(String sourceCode) {
        return HttpResult.ok(receivableMainService.getCode(sourceCode));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody VReceivablePayableMain receivableMain) {
        if(receivableMain == null || receivableMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }

//        if((receivableMain.getDataSource().equals("ReceivableVoucher") || receivableMain.getDataSource().equals("PayableVoucher"))
//                && receivableMain.getPlanList().size() == 0) {
//            throw new NuoNiuException("计划不能为空");
//        }

        receivableMainService.saveReceivable(receivableMain);
        return HttpResult.ok();
    }

    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        receivableMainService.submit(id, status);
        return HttpResult.ok();
    }

    @ApiOperation("单据审核")
    @GetMapping("examine")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        receivableMainService.examine(id, status);
        return HttpResult.ok();
    }

    @ApiOperation("删除单据")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        receivableMainService.deleteById(id);
        return HttpResult.ok();
    }

    @ApiOperation("记账")
    @GetMapping("book")
    public HttpResult book(Long id) {
        receivableMainService.book(id);
        return HttpResult.ok();
    }

    @ApiOperation("获取应付/应付核销列表")
    @GetMapping("getVerificationList")
    public HttpResult<ReceivableVerification> getVerificationList(String dataSource, Integer orderId) {
        return HttpResult.ok(receivableMainService.getVerificationList(dataSource, orderId));
    }

    @ApiOperation("应收单据手动核销")
    @PostMapping("verification")
    public HttpResult verification(@RequestBody ReceivableVerification receivableVerification) {
        if(receivableVerification == null || receivableVerification.getPaymentMainList().size() == 0) {
            throw new NuoNiuException("请选择要核销的收款单");
        }

        if(receivableVerification.getReceivableMainList().size() == 0) {
            throw new NuoNiuException("请选择要核销的应收单据");
        }

        receivableMainService.verification(receivableVerification);
        return HttpResult.ok();
    }

    @ApiOperation("获取计划列表")
    @GetMapping("getPlanList")
    public HttpResult getPlanList(Integer orderId, Integer type) {
        if(orderId == null || type == null) {
            throw new NuoNiuException("订单异常");
        }

        return HttpResult.ok(receivableMainService.getPlanList(orderId, type));
    }
}
